package cn.itcast.order.service;

import cn.itcast.order.client.UserFeign;
import cn.itcast.order.mapper.OrderMapper;
import cn.itcast.order.pojo.Order;
import cn.itcast.order.pojo.User;
import com.alibaba.fastjson.JSON;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
//@DefaultProperties(defaultFallback="allMethodFallBack")
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private UserFeign userFeign;


//    @HystrixCommand(fallbackMethod ="queryOrderByIdFallBack" )
//    @HystrixCommand
    public String queryOrderById(Long orderId) {
        // 1.查询订单
        Order order = orderMapper.findById(orderId);
//        2、根据用户id去远程查询用户信息   硬编码
//        User user = restTemplate.getForObject("http://localhost:8081/user/" + order.getUserId(), User.class);
//        User user = restTemplate.getForObject("http://userservice/user/" + order.getUserId(), User.class);

        User user = userFeign.queryById(order.getUserId());


//      3、把用户信息set到Order中
        order.setUser(user);
        // 4.返回
        return JSON.toJSONString(order);
    }

//
////    降级方法和原方法：1、参数保持一致 2、返回类型保持一致
//    public String queryOrderByIdFallBack(Long orderId) {
//        return "服务器太忙，请稍后重试.....";
//    }
//
//    //    全局降级方法
//    public String allMethodFallBack() {
//        return "服务器太太太太太太太忙，请稍后重试.....";
//    }
}
